package com.anteater.eattheant.view;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.anteater.eattheant.R;
import com.anteater.eattheant.util.Level;

public class ETASurfaceView extends GLSurfaceView {
	private ETARenderer renderer;
	private Context context;

	public interface EndListener {
		public void notify(short score);
	}
	
	public interface SoundListener {
		public void playTonqueSound();
		public void	stopTongueSound();
		public void playCatchAnt();
	}

	public ETASurfaceView(Context context, AttributeSet attributeSet) {
		super(context, attributeSet);
		this.context = context;
		setBackgroundResource(R.drawable.play_bg);
	}

	public void start(Level level, EndListener endListener, SoundListener soundListener) {
		renderer = new ETARenderer(level.getGraph(), level.getAnts(),
				level.getEscapes(), endListener, soundListener, context);
		setRenderer(renderer);
	}

	@Override
	public boolean onTouchEvent(MotionEvent e) {
		float ratio = getHeight() / 3.3f;
		float x = e.getX() / ratio - 2.75f;
		float y = -e.getY() / ratio + 1.65f;

		switch (e.getAction()) {
		case MotionEvent.ACTION_MOVE:
			renderer.moveTongue(x, y);
			break;
		case MotionEvent.ACTION_UP:
			renderer.releaseTongue();
		}

		return true;
	}
}
